package com.lhf.legalPlant.service.impl;

import com.lhf.enumfile.Status;
import com.lhf.error.ErrorMessageVo;
import com.lhf.legalPlant.mapper.FactoryMapper;
import com.lhf.legalPlant.model.*;
import com.lhf.legalPlant.mapper.LegelPersonMapper;
import com.lhf.legalPlant.service.FactoryService;
import com.lhf.util.AllrecordsVo;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author 尐痞猫
 * @date2021-06-25 22:50
 */
@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private LegelPersonMapper legelPersonMapper;

    @Autowired
    private FactoryMapper factoryMapper;


    @Override
    public ResultVo selectByUserId(User user) {

        List<LegelPersonVo> list = legelPersonMapper.selectByUserId(user);

        return ResultBuliderVo.success(list);
    }

    @Override
    public ResultVo query(FactorySelectVo factorySelectVo) {

        //判断是否有法人
        if(factorySelectVo.getLegalPersonCode()==null && factorySelectVo.getLegalPersonCode()=="" ){
            ResultBuliderVo.error("该用户没有法人下属");
        }

        List<FactoryVo> list = factoryMapper.query(factorySelectVo);
        int count = factoryMapper.queryCount(factorySelectVo);
        for(FactoryVo factoryVo:list){
            factoryVo.setEnabledName(Status.getStatusName(factoryVo.getEnabled()));
        }
        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setTotalSize(count);
        allrecordsVo.setPageIndex(factorySelectVo.getPageIndex());
        allrecordsVo.setPageSize(factorySelectVo.getPageSize());
        allrecordsVo.resettotalNum(count);
        allrecordsVo.setDataList(list);

        return ResultBuliderVo.success(allrecordsVo);
    }

    @Override
    @Transactional
    public ResultVo add(@Valid FactoryAddVo factoryAddVo) {
        //判断编码是否重复
        Integer i = factoryMapper.queryByCode(factoryAddVo.getLegalPlantCode());
        if(i!=0){
            return ResultBuliderVo.error(ErrorMessageVo.BIANMAUNENGCHONGFU);
        }
        FactoryVo factoryVo = new FactoryVo();
        BeanUtils.copyProperties(factoryAddVo,factoryVo);
        factoryVo.setEnabled(factoryAddVo.getEnabled()?1:0);
        factoryVo.setCreateName("张三");
        Date now = new Date();
        factoryVo.setCreateTime(now);
        factoryVo.setUpdateName("张三");
        factoryVo.setUpdateTime(now);
        int a = factoryMapper.add(factoryVo);
        return ResultBuliderVo.success(null);
    }

    @Override
    @Transactional
    public ResultVo update(@Valid FactoryUpdateVo factoryUpdateVo) {
        FactoryVo factoryVo = new FactoryVo();
        BeanUtils.copyProperties(factoryUpdateVo,factoryVo);
        factoryVo.setEnabled(factoryUpdateVo.getEnabled()?1:0);
        Date now = new Date();
        factoryVo.setUpdateName("李四");
        factoryVo.setUpdateTime(now);
        int a = factoryMapper.update(factoryVo);
        return ResultBuliderVo.success(null);
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid FactoryUpdateStatusVo factoryUpdateStatusVo) {
        FactoryVo factoryVo = new FactoryVo();
        BeanUtils.copyProperties(factoryUpdateStatusVo,factoryVo);
        factoryVo.setEnabled(factoryUpdateStatusVo.getEnabled()?1:0);
        Date now = new Date();
        factoryVo.setUpdateName("王五");
        factoryVo.setUpdateTime(now);
        int a = factoryMapper.updateStatus(factoryVo);
        return ResultBuliderVo.success(null);
    }

    @Override
    public ResultVo queryFactory(LegelPersonVo legelPersonVo) {
        List<FactoryVo> factoryVos = factoryMapper.queryFactory(legelPersonVo);
        return ResultBuliderVo.success(factoryVos);
    }
}
